package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    /**
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
        //1.获取时间区间内的每一天的时间
        //1.1 定义一个list用来封装每一天的时间
        ArrayList<LocalDate> doubles = new ArrayList<>();
        //1.2插入初始时间
         doubles.add(begin);
        //1.3 插入第一天后的每一天的时间到list集合中
       while (!begin.equals(end)){
           begin=begin.plusDays(1);
           doubles.add(begin);
       }
        //2.5 定义一个list用来封装每一天的营业额
        List<Double>turnoverList=new ArrayList<>();
        //2.根据条件查询的每一天的营业额数据
        for (LocalDate aDouble : doubles) {
            //2.1.获取当天的最小时间，年月日时分秒 00:00
            LocalDateTime of = LocalDateTime.of(aDouble, LocalTime.MIN);
            //2.2 获取当天的最大时间，年月日时分秒 59:59
            LocalDateTime of1 = LocalDateTime.of(aDouble, LocalTime.MAX);
            //2.3 查询mysql
           Map map=  new HashMap<>();
           map.put("status", Orders.COMPLETED);
           map.put("begin",of);
           map.put("end",of1);
            Double aDouble1 = orderMapper.sumByMap(map);
            //注意，此时返回的turnover，有可能为null,如果为null,我们则赋值为0
            aDouble1=aDouble1==null? 0:aDouble1;
            //2.6 把每一天的营业额添加到list中
            turnoverList.add(aDouble1);
        }
        TurnoverReportVO vo = TurnoverReportVO.builder()
                .dateList(StringUtils.join(doubles, ","))
                .turnoverList(StringUtils.join(turnoverList, ","))
                .build();
        return vo;
    }

    /**
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        //定义一个list用来封装每一天的时间
        List<LocalDate> dateList=new ArrayList<>();
        //插入初始时间
        dateList.add(begin);
        // 插入第一天后的每一天的时间到list集合中
        while (!begin.equals(end)){
            begin=begin.plusDays(1);
            dateList.add(begin);
        }
        //定义两个list用来封装每天的新增用户数量和总用户量
        List<Integer>newUser=new ArrayList<>();
        List<Integer>totalUser=new ArrayList<>();
        //查询mysql,查询每一天的新增用户和总用户量
        for (LocalDate localDate : dateList) {
            LocalDateTime of = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime of1 = LocalDateTime.of(localDate, LocalTime.MAX);
            Map map=new HashMap<>();
            map.put("end",of1);
            Integer userCount = userMapper.getUserCount(map);
            map.put("begin",of);
            Integer userCount1 = userMapper.getUserCount(map);
            newUser.add(userCount1);
            totalUser.add(userCount);

        }
        UserReportVO vo = UserReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .newUserList(StringUtils.join(newUser, ","))
                .totalUserList(StringUtils.join(totalUser, ","))
                .build();
        return vo;
    }
    public OrderReportVO orderStatistics(LocalDate begin, LocalDate end) {
        //1.获取时间区间内的每一天的时间
        //1.1 定义一个list用来封装每一天的时间
        List<LocalDate> dateList = new ArrayList<>();
        //1.2插入初始时间
        dateList.add(begin);
        //1.3 插入第一天后的每一天的时间到list集合中
        while (!begin.equals(end)) {//如果当前时间不等于最后一天的话
            begin = begin.plusDays(1);//在指定的时间+1天
            dateList.add(begin);//把每一天的日期加入list集合中
        }
        //2.5 定义两个list用来封装每天的总订单数和有效订单数
        List<Integer> totalOrdersCountList = new ArrayList<>();
        List<Integer> validOrdersCountList = new ArrayList<>();
        //2.查询mysql,查询每天的总订单数和有效订单数
        for (LocalDate date : dateList) {
            //2.1.获取当天的最小时间，年月日时分秒 00:00
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            //2.2 获取当天的最大时间，年月日时分秒 59:59
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            //2.3 查询mysql 获取当天的总订单数  select count(id) from orders where  order_time>#{begin} and order_time<#{end}
            Map map = new HashMap();
            map.put("end", endTime);
            map.put("begin", beginTime);
            Integer totalOrdersCount = orderMapper.getOrderCount(map);
            //2.4查询mysql获取当天的有效订单数量 select count(id) from orders where  order_time>#{begin} and order_time<#{end} and status=5
            map.put("status", Orders.COMPLETED);
            Integer validOrdersCount = orderMapper.getOrderCount(map);
            //2.6 插入数据到list中
            totalOrdersCountList.add(totalOrdersCount);
            validOrdersCountList.add(validOrdersCount);
        }
        //2.7 获取时间区间内的总订单数和有效订单数以及订单完成率
        //聚合求和总订单数
        Integer totalOrdersCount = totalOrdersCountList.stream().reduce(Integer::sum).get();
        //聚合求和有效订单数
        Integer validOrdersCount = validOrdersCountList.stream().reduce(Integer::sum).get();
        //订单完成率=有效订单数/总订单数
        Double orderCompletionRate = 0.0;
        if (totalOrdersCount != 0) {
            orderCompletionRate = validOrdersCount.doubleValue() / totalOrdersCount;
        }
        //3.返回数据
        OrderReportVO vo = OrderReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .orderCountList(StringUtils.join(totalOrdersCountList, ","))
                .validOrderCountList(StringUtils.join(validOrdersCountList, ","))
                .totalOrderCount(totalOrdersCount)
                .validOrderCount(validOrdersCount)
                .orderCompletionRate(orderCompletionRate)
                .build();
        return vo;
    }
    public SalesTop10ReportVO top10(LocalDate begin, LocalDate end){
        //1.转化时间格式
        //1.1.获取当天的最小时间，年月日时分秒 00:00
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        //1.2 获取当天的最大时间，年月日时分秒 59:59
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);

        //2.根据时间区间查询销量(订单表和订单详情表),包含菜品名称，以及对应的销量数
        List<GoodsSalesDTO> goodsSalesDTOS=orderMapper.getSalesTop10(beginTime,endTime);
        //3.获取商品名称列表
        List<String> nameList = goodsSalesDTOS.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        //4.获取销量列表
        List<Integer> numberList = goodsSalesDTOS.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
        //5.返回数据
        SalesTop10ReportVO vo = SalesTop10ReportVO.builder()
                .nameList(StringUtils.join(nameList, ","))
                .numberList(StringUtils.join(numberList, ","))
                .build();
        return vo;
    }
}
